package uiLayer.uiUtilities;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

public class MenuPanel extends JPanel {
	private JPanel buttonGrid;
	private ButtonGroup btnGroup;
	private int buttonCounter;
	private JToggleButton invisibleBtn;
	
	/**
	 * Create the panel.
	 */
	public MenuPanel() {
		this.setBorder(new SoftBevelBorder(BevelBorder.RAISED, null, null, null, null));
		this.setLayout(new BorderLayout(0, 0));
		buttonGrid = buttonGrid();
		this.add(buttonGrid, BorderLayout.WEST);
		btnGroup = new ButtonGroup();
		buttonCounter = 0;
		invisibleBtn = new JToggleButton();
		btnGroup.add(invisibleBtn);
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentHidden(ComponentEvent arg0) {
				invisibleBtn.setSelected(true);
			}
		});
		
	}
	
	public JToggleButton addButton(final String CARD, final JPanel cardContainer, String toolTipText) {
		JToggleButton btn = createButton(CARD, cardContainer);
		btn.setToolTipText(toolTipText);
		btn.setFont(new Font("Calibri", Font.PLAIN, 16));
		btnGroup.add(btn);
		buttonCounter++;
		GridBagConstraints gbc = setButtonConstraints(buttonCounter);
		buttonGrid.add(btn, gbc);
		
		return btn;
		}
	
	/**
	 * Creates a menu button.
	 * 
	 * @param CARD 				The final string cardname.
	 * @param cardConntainer	The final cardContainer panel.
	 * @return 					The button.
	 */
	private JToggleButton createButton(final String CARD, final JPanel cardContainer) {
		JToggleButton tglbtn = new JToggleButton();
		tglbtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				CardLayout cl = (CardLayout)(cardContainer.getLayout());
				cl.show(cardContainer, CARD);
			}
		});
		return tglbtn;
	}
	
	private JPanel buttonGrid() {
		JPanel buttonGrid = new JPanel();
		buttonGrid.setBorder(new EmptyBorder(2, 2, 2, 2));
		
		GridBagLayout gbl_buttonGrid = new GridBagLayout();
		gbl_buttonGrid.columnWidths = new int[] {150, 150, 150, 150};
		gbl_buttonGrid.rowHeights = new int[] {80};
		gbl_buttonGrid.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0};
		gbl_buttonGrid.rowWeights = new double[]{0.0};
		
		buttonGrid.setLayout(gbl_buttonGrid);
		
		return buttonGrid;
	}
	
	/**
	 * Creates the constraints for the menu button.
	 * @param buttonNo		The number of the button.
	 * @return
	 */
	private GridBagConstraints setButtonConstraints(int buttonNo) {
		GridBagConstraints gbc_tglbtn = new GridBagConstraints();
		gbc_tglbtn.fill = GridBagConstraints.BOTH;
		gbc_tglbtn.insets = new Insets(0, 0, 0, 5);
		gbc_tglbtn.gridx = buttonNo - 1;
		gbc_tglbtn.gridy = 0;

		return gbc_tglbtn;
	}

	
}
